package com.nio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Set;

public class NIOClass {
    public void testNio()throws Exception
    {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        SocketChannel socketChannel = serverSocketChannel.accept();
        //1.创建Selector
        Selector selector = Selector.open();
        //2.将channels注册到Selector ，监听读就绪事件
        SelectionKey selectionKey = socketChannel.register(selector, SelectionKey.OP_READ);
        //3.阻塞等待channel有就绪事件; 注册到Selector上的事件是否已就绪；已就绪就返回已就绪的channel个数
        int select = selector.select();
        //获取发生就绪事件的channel集合
        Set<SelectionKey> selectionKeys = selector.selectedKeys();

    }

    /**
     * socket 服务端
     */
    public void socketTest() throws IOException {
        //1.监听端口
        ServerSocket serverSocket = new ServerSocket(8000);
        while (true){
            //2.接受请求建立连接 返回的套接字链接
            Socket accept = serverSocket.accept();
            //3.数据交互
            //new Thread(new BIOServerHandler)
        }
    }

    /**
     * 客户端
     * @throws Exception
     */
    public void socketTestTwo()throws Exception{
        //建立链接，获取套接字
        Socket socket = new Socket("127.0.0.1",80000);
        //获取输入流和输出流
        InputStream inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();

    }
    /**
     * channels使用
     */
    public void serverSocketChannelTest() throws IOException {
        //1.服务器通过服务端socket穿件channel
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        //2.服务器绑定端口 的监听事件
        ServerSocketChannel bind = serverSocketChannel.bind(new InetSocketAddress(8000));
        //3.服务器监听客户端连接，建立SocketChannel连接 和服务器端-客户端建立信息传输的通道
        SocketChannel socketChannel = serverSocketChannel.accept();
        //4.客户端连接远程主机及端口
        SocketChannel socketChannel1 = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8000));

    }

    /**
     * Buffer使用 使用最多的基于byte字节类型的byteBuffer
     */
    public void bufferTest() throws IOException {
        try{
            //初始化长度为10的byte类型buffer
            ByteBuffer byteBuffer = ByteBuffer.allocate(10);

            //向byteBuffer写入3个字节
            ByteBuffer put = byteBuffer.put("abc".getBytes(Charset.forName("utf-8")));

            //将byteBuffer从写模式切换成读模式
            byteBuffer.flip();
            //从bytebuffer中读取一个字节
            byte b = byteBuffer.get();
            System.out.println("byte:"+b);
            //调用mark方法记录一下当前position的位置
            byteBuffer.mark();
            //先调用一下get() 读取一个字节
            //再调用reset方法将position重置到mark位置
            byteBuffer.get();
            byteBuffer.reset();
            /**
             * 调用clear方法重置
             */
            byteBuffer.clear();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
